/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.benmei.weike.web.weixin;

import com.benmei.weike.common.Constants;
import com.benmei.weike.common.ResultCode;
import com.benmei.weike.service.weixin.JsapiSignService;
import com.benmei.weike.util.JsonUtil;
import com.benmei.weike.web.vo.weixin.JsapiSignResult;
import com.benmei.weike.web.vo.weixin.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 管理员管理用户的Controller.
 *
 * @author peter
 */
@Controller
@RequestMapping(value = "/jsapi")
public class WeixniJsApiController {

    private static Logger logger = LoggerFactory.getLogger(WeixniJsApiController.class);

    @Autowired
    private JsapiSignService jsapiSignService;

//    @Autowired
//    private UserService userService;

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String list(Model model) {
        return "weix-js-api-test/test";
    }

    // jsapi 接入
    @RequestMapping(value = "ajax/sign", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult jsapiSign(@ModelAttribute("url") String url) {
        //打印入参
        logger.info(Constants.prefix_in + url);
        JsapiSignResult result = new JsapiSignResult();

        try {
            result = jsapiSignService.sign(url);
//            String openid = UserUtil.getCurrentUserOpenId();
//            if(StringUtils.isNotBlank(openid)) {
//                result.setOpenid(openid);
//                IndexData data = userService.getIndexData(openid);
//                result.setQuantityOfVocabulary(data.getQuantityOfVocabulary());
//                result.setStudyDays(data.getStudyDays());
//            }else{
//                logger.error("jsapi签名时获取用户信息失败：用户未登录");
//            }

        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage(ResultCode.ERROR_MSG);
            result.setCode(ResultCode.ERROR);
            logger.error("---------- jsapi 接入   --------", e);
        }

        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(result));
        return result;
    }


}
